﻿namespace NServiceBus.Core.Tests.Timeout;

using System;
using NUnit.Framework;

[TestFixture]
public class DelayedDeliveryOptionExtensionsTests
{
    [Test]
    public void GetDeliveryDelayShouldReturnTheConfiguredDelayTimeSpan()
    {
        var options = new SendOptions();
        var delay = TimeSpan.FromMinutes(42);
        options.DelayDeliveryWith(delay);

        Assert.That(options.GetDeliveryDelay(), Is.EqualTo(delay));
    }

    [Test]
    public void GetDeliveryDelayShouldReturnNullWhenNoDelayConfigured()
    {
        var options = new SendOptions();

        Assert.That(options.GetDeliveryDelay(), Is.Null);
    }

    [Test]
    public void GetDeliveryDateShouldReturnTheConfiguredDeliveryDate()
    {
        var options = new SendOptions();
        var deliveryDate = new DateTimeOffset(2012, 12, 12, 12, 12, 12, TimeSpan.Zero);
        options.DoNotDeliverBefore(deliveryDate);

        Assert.That(options.GetDeliveryDate(), Is.EqualTo(deliveryDate));
    }

    [Test]
    public void GetDeliveryDateShouldReturnNullWhenNoDateConfigured()
    {
        var options = new SendOptions();

        Assert.That(options.GetDeliveryDate(), Is.Null);
    }

    [Test]
    public void DelayDeliveryWithShouldThrowExceptionWhenDoNotDeliverBeforeAlreadyExists()
    {
        var options = new SendOptions();
        options.DoNotDeliverBefore(DateTimeOffset.UtcNow.AddDays(1));

        Assert.Throws<InvalidOperationException>(() => options.DelayDeliveryWith(TimeSpan.FromDays(1)));
    }

    [Test]
    public void DoNotDeliverBeforeShouldThrowExceptionWhenDelayDeliveryWithAlreadyExists()
    {
        var options = new SendOptions();
        options.DelayDeliveryWith(TimeSpan.FromDays(1));

        Assert.Throws<InvalidOperationException>(() => options.DoNotDeliverBefore(DateTimeOffset.UtcNow.AddDays(1)));
    }
}